// CommandTypeEnum
var adCmdUnspecified        = -1;
var adCmdText               = 1;
var adCmdTable              = 2;
var adCmdStoredProc         = 4;
var adCmdUnknown            = 8;
var adCmdFile               = 256;
var adCmdTableDirect        = 512;

// ExecuteOptionEnum
var adAsyncExecute          = 0x10;
var adAsyncFetch            = 0x20;
var adAsyncFetchNonBlocking = 0x40;
var adExecuteNoRecords      = 0x80;
var adExecuteStream         = 0x400;
var adOptionUnspecified     = -1;


var dbName = "OSH.mdb";
if(WScript.Arguments.length > 0)
	dbName = WScript.Arguments(0);

var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + dbName;

var fso = new ActiveXObject("Scripting.FileSystemObject");

// Create database if it's not exists
if(fso.FileExists(dbName))
{
//	WScript.echo("Database already exists.");
//	WScript.quit();
	fso.DeleteFile(dbName);
}


CreateNewDatabase(connectionString);

WScript.quit();


function CreateNewDatabase(connectionString)
{
	var catalog = new ActiveXObject("ADOX.Catalog");
	catalog.Create(connectionString);


	// Connect to the database
	var connection = new ActiveXObject("ADODB.Connection");
	connection.Open(connectionString);
	catalog.ActiveConnection = connection;


	// Create database tables
	var sql = "create table EitEvents (" +
		"Id autoincrement," +
		"NetworkId int," +
		"TransportId int," +
		"ServiceId int," +
		"EventId int," +
		"StartTime datetime," +
		"EndTime datetime," +
		"Duration int," +
		"[Language] char(3)," +
		"EventName nvarchar(255)," +
		"[Text] nvarchar(255)" +
		")";

	connection.Execute(sql, 0, adCmdText + adExecuteNoRecords);


	var sql = "CREATE UNIQUE INDEX PK_EitEvents ON EitEvents " +
		"(NetworkId, TransportId, ServiceId, EventId, [Language]) " +
		"WITH PRIMARY";

	connection.Execute(sql, 0, adCmdText + adExecuteNoRecords);
	

	var sql = "CREATE INDEX IX_EitEvents_NTSLSE ON EitEvents " +
		"(NetworkId, TransportId, ServiceId, [Language], StartTime)";

	connection.Execute(sql, 0, adCmdText + adExecuteNoRecords);
}



function ShowUsage()
{
	WScript.echo("Usage: " + WScript.ScriptName + " databasename.mdb");
	WScript.quit();
}








